// manager/pages/deleteprocess/index.js
import { request, uploadFile } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 上一页数据
        versionId: "",
        instanceId: "",
        stepId: "",
        taskId: "",
        keyInfo: "",
        currentComponent: "",
        stepList: [
            // {
            // 	id: 1,
            // 	text: "基本信息",
            // 	component: "basicinfo",
            // 	success: false,
            // 	selected: false
            // },
            // {
            // 	id: 2,
            // 	text: "客户信息",
            // 	component: "custinfo",
            // 	success: false,
            // 	selected: false
            // },
            // {
            // 	id: 3,
            // 	text: "家庭成员",
            // 	component: "familyInfo",
            // 	success: false,
            // 	selected: false
            // },
            // {
            // 	id: 4,
            // 	text: "影像资料",
            // 	component: "imageData",
            // 	success: false,
            // 	selected: false
            // },
            // {
            // 	id: 5,
            // 	text: "影像资料",
            // 	component: "imageData",
            // 	success: false,
            // 	selected: false
            // },
            {
            	id: 6,
            	text: "影像资料",
            	component: "imageData",
            	success: false,
            	selected: false
            }
        ],
        readonly: false,

        stepId: "1",
        imageData: [],

        // 审批意见
        showOpinion: false,
        opinionForm: {
            approveType: "",
            approveMemo: "",
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let { versionId, instanceId, stepId, taskId, keyInfo } = options;
        this.setData({
            versionId,
            instanceId,
            stepId,
            taskId,
            keyInfo: decodeURIComponent(options.keyInfo)
        })
        this.dealWithWaitTask();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /** 切换 */
    stepChange(e){
        let { index } = e.detail;
        let { currentComponent, stepList } = this.data;
        if(currentComponent != stepList[index].component){
            this.setStepSeleced(index);
        }


        // var basicinfo = this.selectComponent('#basicinfo')
        // var custinfo = this.selectComponent('#custinfo')
        // console.log(basicinfo.validationForm())
        // console.log(custinfo.s())
    },
    /** 检查是否全部点击查看 */
    isAllClick(){
        let { stepList } = this.data;
        const item = stepList.find(item => item.isClick == false);
        if(!item){
            return true
        }else{
            wx.showToast({
                title: item.text + '页签需要审阅！', 
                icon: "none"
            });
            return false
        }
    },
    /** 检查是否全部填完信息 */
    isAllSuccess(){
        let { stepList } = this.data;
        const item = stepList.find(item => item.success == false);
        if(!item){
            return true
        }else{
            // if(item.component == 'applyInfo'){
            //     if(this.$refs.applyInfo.validationForm()){
            //         wx.showToast({
            //             title: '请添加' + item.text + '！', 
            //             icon: "none"
            //         });
            //     } 
            //     if(this.applyInfo.authSts == 0){
            //         wx.showToast({
            //             title: '请完成' + item.text + '授权！', 
            //             icon: "none"
            //         });
            //     }
            // }else{
            //     wx.showToast({
            //         title: '请添加' + item.text + '！', 
            //         icon: "none"
            //     });
            // }
            return false
        }
    },
    /** 设置选中 */
    setStepSeleced(index){
        let { currentComponent, stepList } = this.data;
        stepList.map((item,i) => {
            if(index == i){
                currentComponent = stepList[index].component;
                item.isClick = true;
                item.selected = true;
            }else{
                item.selected = false;
            }
        })
        this.setData({
            currentComponent, 
            stepList
        })
    },
    /** 设置完成 */
    setStepSuccess(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.success = true;
            }
        })
        this.setData({
            stepList
        })
    },
    /** 设置未完成 */
    setStepFail(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.success = false;
            }
        })
        this.setData({
            stepList
        })
    },
    /** 设置下一个Component */
    setNextComponent(){
        let { currentComponent, stepList } = this.data;
        let index = stepList.findIndex(item => item.component == currentComponent);
        if(index < stepList.length - 1){
            this.setStepSeleced(index+1);
        }
    },
    /** 设置当前组件请求完成 */
    setComponentHideLoading(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.showLoading = false;
            }
        })
        this.setData({
            stepList
        })
    },

    /** 启动流程 */
    dealWithWaitTask() {
        let { versionId, instanceId, stepId, taskId } = this.data;
        request({
            url: `/businessFlow/dealWithWaitTask`,
            // method: "GET",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                versionId, 
                instanceId, 
                stepId, 
                taskId
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)

                let pageList = res.data.pageList;
				let stepList = [];
                pageList.forEach((item,index) => {
                    stepList.push({
                        id: item.stepId,
                        text: item.pageName,
                        component: item.appPageUrl,
                        versionId: item.versionId,
                        orderNum: item.orderNum,
                        isOnlyRead: item.isOnlyRead,
                        isCheck: item.isCheck,
                        showLoading: true,
                        success: false,
                        isClick: index == 0 ? true : false,
                        selected: index == 0 ? true : false,
                    })
                })
                stepList.push({
                    id: 'ls_001',
                    text: '影像资料',
                    component: 'imageData',
                    showLoading: true,
                    success: false,
                    isClick: stepList.length == 0 ? true : false,
                    selected: stepList.length == 0 ? true : false,
                })
                this.setStepSeleced(0);
                this.currentComponent = stepList[0].component;
                stepList.forEach((item,index) => {
                    this.getInitData(item.component);
                })
                this.setData({
                    appAuthorList: res.data.appAuthorList,
                    stepList
                })
            }
        }).catch(error => {
            console.log(error)
        })
    },

    /** 获取对应数据 */
    getInitData(component){
        console.log(component)
        switch(component){
            case 'imageData':
                this.getDocRegList();
                break;
            default:
                // this.setComponentHideLoading(component);
                break;
        }
    },

    /** 影像资料 */
    /** 获取影像资料类型列表 */
    getDocRegList() {
        let { versionId, instanceId } = this.data;
        request({
            url: '/businessDoc/getDocRegList',
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            showLoading: false,
            data: {
                versionId, 
                instanceId
            }
        }).then(res => {
            console.log('getDocRegList')
            console.log(res)
            if(res.code == 0){
                let imageData = res.data;
                this.setData({
                    imageData
                })
                imageData.forEach((item,index) => {
                    this.getDocList(item,index);
                })
                if(imageData.length == 0){
                    this.setStepSuccess("imageData");
                }                
                // this.setComponentHideLoading('imageData');
            }
        })
    },
    /** 获取影像资料列表 */
    getDocList(item,index) {
        console.log(item,index)
        let { imageData } = this.data;
        request({
            url: `/businessDoc/getDocList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            showLoading: false,
            data: {
                docRegId: item.docRegId
            }
        }).then(res => {
            console.log('getDocList')
            console.log(res)
            if(res.code == 0){
                item.list = res.data;
                imageData.splice(index,1,item)
                this.setData({
                    imageData
                })
                if(index == imageData.length - 1){
                    if(this.uploadsCheck(imageData)){
                        this.setStepSuccess("imageData");
                    }else{
                        this.setStepFail("imageData");
                    }
                }
            }
        })
    },
    /** 影像资料--上传图片 */
    upload(e){
        console.log(e.detail)
        let { index, location, file, docCode, docRegId } = e.detail;
        uploadFile({
            url: '/businessDoc/uploadDocFile',
            filePath: file,
            name: 'file',
            formData: {
                docCode: docCode,
                docRegId: docRegId,
                instanceId: this.data.instanceId,
                location: location,
            },
        }).then(res => {
            // console.log("KKKKKKKKK")
            // console.log(res)
            if(res.code == 0){
                this.getDocList(this.data.imageData[index],index)
            }
        })
    },
    /** 影像资料--删除图片 */
    deleteImage(e){
        console.log(e.detail)
        let { index, item } = e.detail;
        let that = this;
        wx.showModal({
            title: '确认删除',
            content: '您确认删除该图片吗',
            success (res) {
                if (res.confirm) {
                    request({
                        url: '/businessDoc/delDocList',
                        reqRSAENCRY: true, // 参数是否加密
                        resRSADECRY: false, // 返回值是否解密
                        data: {
                            id: item.id,
                            docRegId: item.docRegId,
                        }
                    }).then(res => {
                        if(res.code == 0){
                            wx.showToast({
                                title: "删除成功",
                                icon: "none"
                            });
                            that.getDocList(that.data.imageData[index],index)
                        }
                    })
                    console.log('用户点击确定')
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
        // this.deleteImageData = data;
        // this.deleteImageShow = true;
    },
    /** 影像资料--数据校验 */
    uploadsCheck(list){
        let flag = true;
        list.forEach(item => {
            if(item.stepId == this.data.stepId && item.isMust == "T" && item.list.length == 0){
                flag = false;
            }
        })
        return flag;
    },

    /** 显示提交意见 */
    showOpinionPopup(){
        this.setData({
            showOpinion: true,
        })
    },

    /** 提交意见输入框输入时 */
    opinionInput(e){
        let key = e.target.dataset.key;
        this.setData({
            ['opinionForm.'+key]: e.detail.value
        })   
    },

    /** 提交意见选择框变化时 */
    opinionPickerChagne(e){
        let { key, value } = e.detail;
        this.setData({
            // array[e.detail.value].approvCode + (array[e.detail.value].returnStepId || '');
            ['opinionForm.'+key]: value
        })
    },
    /** 提交意见取消 */
    cancelOpinion(){
        this.setData({
            showOpinion: false,
        })
    },
    /** 提交意见确认 */
    confirmOpinion(){
        let { opinionForm, stepList } = this.data;
        //表单校验
        if (!opinionForm.approveType) {
            wx.showToast({ title: "请选择审批意见!", icon: "none" });
            return
        }
        if (opinionForm.approveType != 'T' && opinionForm.approveMemo == '') {//如果选择了拒件，拒件理由为必填
            wx.showToast({ title: "请输入意见说明!", icon: "none" });
            return;
        }
        this.setData({
            showOpinion: false
        })
        // 校验页面必填和TAB页打开，如果是同意的时候才进行提示，退回和拒件的不进行提示
        if(opinionForm.approveType == "T"){
            console.log("TTTTTTTTTTT")
            //验证数据是否填写完善
            let index = stepList.findIndex(value => value.success == false);
            // console.log(this.stepList)
            console.log(index)
            if(index != -1){
                let item = stepList[index];
                this.setData({
                    currentComponent: item.component
                })
                this.setStepSeleced(index);
                if(item.component == "imageData"){
                    if(!this.uploadsCheck(this.data.imageData)){
                        wx.showToast({
                            title: '请上传' + item.text + '！',  
                            icon: "none" ,
                        });
                        return;
                    }
                }
            }else{
                if(!this.isAllClick() || !this.isAllSuccess()){
                    return
                }
            }
        }
        this.submitTask();
    },
    /** 提交待办任务 */
    submitTask(){
        let { versionId, instanceId, stepId, taskId, keyInfo, opinionForm } = this.data;
        request({
            url: `/businessFlow/submitTask`,
            // method: "GET",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
            data: {
                versionId,
                instanceId,
                stepId,
                taskId,
                keyInfo,
                approveType: opinionForm.approveType,
                approveMemo: opinionForm.approveMemo,
            }
        }).then(res => {
            if(res.code == 0){
                wx.navigateBack();
            }
        }).catch(error => {
            console.log(error)
        })
    },
})